//全局变量
var itemCount = 0;


//includes functions
function ltrim(str){
	var i;
	for(i=0;i<str.length;i++){
		if(str.charAt(i)!=" ") break;
	}
	str=str.substring(i,str.length);
	return str;	
}
function rtrim(str){
	var i;
	for(i=str.length-1;i>=0;i--){
		if(str.charAt(i)!=" ") break;
	}
	str=str.substring(0,i+1);
	return str;
}
//去除字符串两端空白字符
function trim(str){
	str=rtrim(ltrim(str));
	return str;
}
function checkEmpty(str){
	if(str=="") return true;
	else return false;
}
function mouseOnfocuseStyle(o,cssText){
	o.style.cssText = cssText;
}
function mouseOnblurStyle(o,cssText){
	o.style.cssText = cssText;
}
//获取元素位置
function getElementPositon(ob){
	var leftP = ob.getBoundingClientRect().left;
	var topP = ob.getBoundingClientRect().top;
	var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
	var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
	leftP +=scrollLeft;
	topP +=scrollTop;
	return {x:leftP,y:topP};
}
//end includes

function loginSubmitCheck(){
	var userNumber = document.getElementsByName("userNumber")[0].value;
	var userPwd = document.getElementsByName("userPwd")[0].value;
	var yanZhengMa = document.getElementsByName("yanZhengMa")[0].value;
	var oYanZhengMa = document.getElementsByName("oYanZhengMa")[0].value;
	userNumber = trim(userNumber);
	userPwd = trim(userPwd);
	yanZhengMa = trim(yanZhengMa);
	oYanZhengMa = trim(oYanZhengMa);
	if(checkEmpty(userNumber)){
		alert("用户账号不能为空！");
		document.getElementsByName("userNumber")[0].focus();
		return false;
	}
	if(checkEmpty(userPwd)){
		alert("用户密码不能为空！");
		document.getElementsByName("userPwd")[0].focus();
		return false;
	}
	if(yanZhengMa!=oYanZhengMa){
		window.location.href = "index.php?userNumber="+userNumber+"&&error=2";
		document.getElementsByName("userPwd")[0].focus();
		return false;
	}

}
function setMouseOnfouseStyle(o){
	var cssText = "background-color:#FFFFCC;";
	mouseOnfocuseStyle(o,cssText);
}
function setMouseOnblurStyle(o){
	var cssText = "background-color:#DBDBDB;";
	mouseOnblurStyle(o,cssText);
}
function addNewFriendMes(){
	document.getElementById("addFriend").style.cssText = "display:block;";
}
function addNewFenLei(o){
	createbackground();
	var addFriend = document.getElementById("addFriend");
	var position = getElementPositon(o);
	var fenLeiDiv = document.createElement("div"); 
	fenLeiDiv.setAttribute("id","feileidiv");
	var leftP = position.x-300;
	var topP = position.y-100;
	fenLeiDiv.style.cssText = "display:none;z-index:1001;position:absolute;left:"+leftP+"px;top:"+topP+"px;width:498px;";
	fenLeiDiv.innerHTML = "<div id='mainAddFenLei'><div id='mainAddFenLeitop' onmousedown=\"getDragElement('feileidiv')\"><span id='mainAddFenLeitopText'>新增分类</span><img src='style/close.jpg' class='closejpg' onclick='closeFeiLeiDiv(\"feileidiv\")' onmouseover='closeOverStyle(this)' onmouseout='closeOutStyle(this)' /></div><div id='mainAddFenLeibody'>分类名称：<input type='text' name='newFeiLei' id='newFeiLei' class='loginuserzhanghao8' onfocus=\"setMouseOnfouseStyle(\'newFeiLei\')\" onblur=\"setMouseOnblurStyle(\'newFeiLei\')\" /><br /><input type='button' value='确定' class='addFeiLeiButton' onclick='addNewFriendClass()' /><input type='button' value='取消' class='addFeiLeiButton' onclick='closeFeiLeiDiv(\"feileidiv\")' /></div></div><div id='leftAddFenLei'></div><div id='bottomAddFenLei'></div>";
	addFriend.appendChild(fenLeiDiv);
	$("#feileidiv").slideDown("slow");
	
}
function closeFeiLeiDiv(ElementId){
	var addFriend = document.getElementById("addFriend");
	var addFeiLeidiv = document.getElementById(ElementId);
	addFriend.removeChild(addFeiLeidiv);
	var bg = document.getElementById("bgchop");
	document.body.removeChild(bg);
}
function closeaddFreind(){
	document.getElementById("addFriend").style.cssText="display:none;";
	document.getElementById("friendviewcontent").style.display = "block";
}
function closeOverStyle(ob){
	ob.style.cssText = "opacity:100;filter:Alpha(Opacity=\"100\",FinishOpacity=\"100\",Style=\"2\");";
}
function closeOutStyle(ob){
	ob.style.cssText = "opacity:0.5;filter:Alpha(Opacity=\"50\",FinishOpacity=\"50\",Style=\"2\");";
}
var xmlHttp;
function addNewFriendClass(){
	xmlHttp = ajaxClass();
	var className = document.getElementById("newFeiLei").value;
	if(className==''){
		alert("类名不能为空！");
		return false;
	}
	className = encodeURIComponent(className);
	var url = "addFriendClass.php?className="+className;
	sendGetRequest(url);
}
function newItem(){
 	itemCount++;
 	var newDivId = "item"+itemCount;
 	var newDiv = document.createElement("div");
 	newDiv.setAttribute("id",newDivId);
 	var initData = document.getElementById("itemInit").innerHTML;
 	newDiv.innerHTML = initData;
 	var divsParent = document.getElementById("addFriendForm");
 	var refid = itemCount-1;
 	var refdiv = document.getElementById("item"+refid);
 	refdiv =refdiv.nextSibling;
 	divsParent.insertBefore(newDiv,refdiv);
 }

 function changeList(ob){
 	ob.style.cssText = "width:990px;margin:0 auto;background:#4A9A30;";
 }
 function changeListt(ob){
 	var orignal = ob.getAttribute("tid");
 	if(orignal%2==0){
 	ob.style.cssText = "width:899px;margin:0 auto;background:#A7C569;";
 	}
 	else{
 		ob.style.cssText = "width:899px;margin:0 auto;background:#FFFFCC;";
 	}
 	var childNum = ob.childNodes.length;
 	for(var i=0;i<childNum;i++){
 		ob.childNodes[i].style.cssText="float:left;padding-left:5px;padding-top:9px;padding-right:5px;border:1px solid #666666;width:100px;height:30px;overflow:hidden;text-align:center;";
 	}
 }
  function changeLiList(ob){
 	ob.style.cssText = "width:auto;min-width:130px;max-width:150px;height:40px;background:#53CDD0;overflow:visible;word-wrap:break-word;word-break:break-all;font-weight:bolder;";
  }
   function changeLiListt(ob){
   	ob.style.cssText = "width:100px;height:30px;background:none;overflow:hidden;word-wrap:break-word;word-break:break-all;font-weight:normal;";
   }
   
   
function createAjaxClass(){
var xmlHttp;
try{
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
}catch (e){
  // Internet Explorer
   try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e){
      try{
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e){
         alert("您的浏览器不支持AJAX！");
         return false;
         }
      }
    }
return xmlHttp;
} 
   
   function addfinanceterm(){
   	var fangxiang = document.getElementById("fangxiang").value;
   	var kind = document.getElementById("kind").value;
   	var title = document.getElementById("title").value;
   	var quantity = document.getElementById("quantity").value;
   	var beizhu = document.getElementById("beizhu").value;
   	var postStr ="fangxiang="+fangxiang+"&kind="+kind+"&title="+title+"&quantity="+quantity+"&beizhu="+beizhu;
   	var xmlHttp = createAjaxClass();
   	xmlHttp.open("post","financemanageajax.php",true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = function(){
		 if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		 	if(quantity==""){
		 		quantity=0.0;
		 	}
		 	 if(fangxiang==0){
		 	 	var oldzhichu = document.getElementById("finacezongzhichu").value;
		 	 	//oldzhichu = parseFloat(oldzhichu);
				var dataformfinancezhichufirst = parseFloat(quantity)+parseFloat(oldzhichu);	
		 	 	document.getElementById("finacezongzhichu").value =dataformfinancezhichufirst.toFixed(2);
		 	 	//alert(oldzhichu);
		 	 	var finacezongzhichu = document.getElementById("finacezongzhichu").value;
		 	 	var finacezongshouru = document.getElementById("finacezongshouru").value;
		 	 	finacezongzhichu = parseFloat(finacezongzhichu);	
		 	 	finacezongshouru = parseFloat(finacezongshouru);
				var dataformulafinancefirstyue =finacezongshouru-finacezongzhichu;
		 	 	document.getElementById("finacezongyue").value = dataformulafinancefirstyue.toFixed(2);
		 	 }
		 	 else{
		 	 	var oldshouru = document.getElementById("finacezongshouru").value;
		 	 	oldshouru = parseFloat(oldshouru);
				var formdatazongshouru = parseFloat(quantity)+oldshouru;	
		 	 	document.getElementById("finacezongshouru").value = formdatazongshouru.toFixed(2);
		 	 	var finacezongzhichu = document.getElementById("finacezongzhichu").value;
		 	 	var finacezongshouru = document.getElementById("finacezongshouru").value;
		 	 	
		 	 	finacezongzhichu = parseFloat(finacezongzhichu);	
		 	 	finacezongshouru = parseFloat(finacezongshouru);
				var dataformfinanceyue = finacezongshouru-finacezongzhichu;
		 	 	document.getElementById("finacezongyue").value = dataformfinanceyue.toFixed(2);
		 	 }
  			  document.getElementById("kind").value = "";
			  document.getElementById("title").value = "";
			  document.getElementById("quantity").value = "";
			  document.getElementById("beizhu").value = "";
			  //alert('ss');
			  var div = document.getElementById("displaymesarea");
			  div.innerHTML = "<h1 style='color:green;text-align:center;font-size:20px;'>添加成功</h1>";
			  setTimeout("setDisplayMesEmpty()",3000);
			  
   		} 
	}
	xmlHttp.send(postStr);
   }
function setDisplayMesEmpty(){
	document.getElementById("displaymesarea").innerHTML=""
}
var oldfinancechangenot;
function cannotchangefocus(ob){
	oldfinancechangenot = ob.value
}
function cannotchangeblur(ob){
	ob.value = oldfinancechangenot;
}
var xmlHttp;
var startdate;
var enddate;
function financesearch(){
	startdate = document.getElementById("startdate").value;
	enddate = document.getElementById("enddate").value;
	//reset style
	$("#startdate").css({"border":"1px solid grey"});
	$("#enddate").css({"border":"1px solid grey"});
	var check = HHL.dateVerify(startdate);//&&HHL.dateVerify(enddate);
	if(!check){
		HHL.newAlertMes("日期格式不正确！","color:red;","1500");
		$("#startdate").css({"border":"1px solid red"});
		$("#financesearchResult").html("");
		$("#startdate").focus();
		return false;
	}
	var check = HHL.dateVerify(enddate);
	if(!check){
		HHL.newAlertMes("日期格式不正确！","color:red;","1500");
		$("#enddate").css({"border":"1px solid red"});
		$("#financesearchResult").html("");
		$("#enddate").focus();
		return false;
	}
	var searchfx = document.getElementById("searchfangxiang").value;
	var searchkind;
	if(searchfx == "0"){
		searchkind = document.getElementById("searchformzkinds").value;
	}else if(searchfx =="1"){
		searchkind = document.getElementById("searchformskinds").value;
	}
	else {
	searchkind = "all"
 	}
	var postStr = "startdate="+startdate+"&enddate="+enddate+"&searchfx="+searchfx+"&searchkind="+searchkind;
	//alert(postStr);
	//var postStr = "startdate="+startdate+"&enddate="+enddate;
	xmlHttp = createAjaxClass();
	xmlHttp.open("post","financesearchajax.php",true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//xmlHttp.onreadystatechange =null;
	xmlHttp.onreadystatechange = financesearchresponse;
	xmlHttp.send(postStr);
}
function financesearchresponse(){
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		var xmlresponse=xmlHttp.responseXML;
    	var xmldocument=xmlresponse.documentElement;
    	var items = xmldocument.childNodes;
    	var divob = document.getElementById("financesearchResult");
    	divob.innerHTML="<h3 class='finstitlesearch'>您从"+startdate+"至"+enddate+"的财务收入支出明细</h3><ul id='finstitle'><li>收入/支出</li><li>类别</li><li>标题</li><li>数额</li><li>备注</li><li>时间</li></ul><div style='clear:both;'></div>";
    	htmlsearchcontent = "<ul id='finscontent'>";
    	zhichuThisSearch = 0.0;
    	shouruThisSearch = 0.0;
    	if(items.length>0){
    		var wholelen = items.length;
			for(var i=0;i<wholelen;i++){
				if(items[i].childNodes[0].childNodes.length>0){
					var fx       = items[i].childNodes[0].firstChild.nodeValue;
					if(fx==0)fx = "支出";
					if(fx==1)fx = "收入";
					
				}
					
				if(items[i].childNodes[1].childNodes.length>0)	
					var kind     = items[i].childNodes[1].firstChild.nodeValue;
				else var kind = " ";
				if(items[i].childNodes[2].childNodes.length>0)
					var title    = items[i].childNodes[2].firstChild.nodeValue;
				else var title = " ";
				if(items[i].childNodes[3].childNodes.length>0){
					var quantity = items[i].childNodes[3].firstChild.nodeValue;
					if(fx=="支出"){
						zhichuThisSearch=parseFloat(zhichuThisSearch)+parseFloat(quantity);
					}
					else{shouruThisSearch=parseFloat(shouruThisSearch)+parseFloat(quantity);		
				    }
				}
				else var quantity = 0;
				if(items[i].childNodes[4].childNodes.length>0)
					var beizhu   = items[i].childNodes[4].firstChild.nodeValue;
				else var beizhu = " ";
				if(items[i].childNodes[5].childNodes.length>0)
					var finstime = items[i].childNodes[5].firstChild.nodeValue;
				else var finstime = " ";
				htmlsearchcontent += "<li title="+fx+">"+fx+"</li><li title="+kind+">"+kind+"</li><li title="+title+">"+title+"</li><li title="+quantity+">"+quantity+"￥</li><li title="+beizhu+">"+beizhu+"</li><li title="+finstime+">"+finstime+"</li><div style='clear:both;'></div>";	
		   }
			zhichuThisSearch = zhichuThisSearch.toFixed(2);
			shouruThisSearch = shouruThisSearch.toFixed(2);
			htmlsearchcontent +="</ul><div id='endfinsearchh3inc'><h3 class='finsh3'>总共支出："+zhichuThisSearch+"￥</h3><h3 class='finsh3'>总共收入："+shouruThisSearch+"￥</h3></div>";
			divob.innerHTML +=htmlsearchcontent;
    	}
    	else{
    		divob.innerHTML="<h3 style='float:left;padding:20px;color:green;width:855px;border:1px solid grey;'>没有查到您需要的数据!<br />您可以检查日期格式（如：2010-1-1）或日期范围,:)</h3>";
    	}
    }
	
}
function makeConfirm(){
var fx = document.getElementById("fangxiang").value;
var formkind = document.getElementById("kind").value;
var formtitle = document.getElementById("title").value;
var quantity = document.getElementById("quantity").value;
if(formkind==""){
alert("项目种类不能为空！");
document.getElementById("kind").focus();
return false;
}
if(formtitle==""){
alert("项目标题不能为空！");
document.getElementById("title").focus();
return false;
}
if(fx == 0){
fx = "支出";
}
else{
fx = "收入";
}
if(quantity==""){
quantity = 0;
}
var disMsg = fx.toString()+" "+formkind.toString()+" "+formtitle.toString()+" "+quantity.toString();
var hadler = confirm("您将"+disMsg+"元\n提交后不能被删除!\n"+"确定提交吗？");
if(hadler){
addfinanceterm();
}
else{
return false;
}

}
function financegetkinds(paravalue){
var addkind = paravalue;
document.getElementById("kind").value = addkind;
}
function displaykinds(){
var addfx = document.getElementById("fangxiang").value;
var addshouru = document.getElementById("financeskinds");
var addzhichu = document.getElementById("financezkinds");
if(addfx == 0){
addshouru.style.cssText ="display:none;";
addzhichu.style.cssText ="display:block;width:390px;float:left;margin-left:10px;font-size:11px;color:green;";
}else{
addshouru.style.cssText ="display:block;width:390px;float:left;margin-left:10px;font-size:11px;color:green;";
addzhichu.style.cssText ="display:none;";
}

}

function searchdisplaykinds(){
var searchtitlefx = document.getElementById("searchfangxiang").value;
var searchskinds = document.getElementById("searchformskinds");
var searchzkinds = document.getElementById("searchformzkinds");
if(searchtitlefx=="0"){
searchskinds.style.cssText = "display:none;";
searchzkinds.style.cssText = "display:block;float:left;";
}else if(searchtitlefx=="1"){
searchskinds.style.cssText = "display:block;float:left;";
searchzkinds.style.cssText = "display:none;";
}
else{
searchskinds.style.cssText = "display:none;";
searchzkinds.style.cssText = "display:none;";
}


}
function createbackground(){
	var bg = document.getElementById("bgchop");
	if(!bg){
		bg = document.createElement("div");
	}
	bg.setAttribute("id","bgchop");
	var screen = {'x':0,'y':0};
	screen.x = window.screen.width||document.body.width;
	screen.y = window.screen.height||(window.screen.availHeight+document.body.scrollHeight)||document.body.height;
	//alert(screen.y);
	bg.style.cssText = "position:absolute;left:0px;top:0px;background-color:grey;z-index:1000;opacity:0.5;filter:alpha(opacity=50);width:"+screen.x+"px;height:"+screen.y+"px;";
	document.body.appendChild(bg);
}
function closeBgchop(){
	var bg = document.getElementById("bgchop");
	if(bg){
		document.body.removeChild(bg);
	}
}
/*
页可见区域宽： document.body.clientWidth
网页可见区域高： document.body.clientHeight
网页可见区域宽： document.body.offsetWidth (包括边线的宽)
网页可见区域高： document.body.offsetHeight (包括边线的高)
网页正文全文宽： document.body.scrollWidth
网页正文全文高： document.body.scrollHeight
网页被卷去的高： document.body.scrollTop
网页被卷去的左： document.body.scrollLeft
网页正文部分上： window.screenTop
网页正文部分左： window.screenLeft
屏幕分辨率的高： window.screen.height
屏幕分辨率的宽： window.screen.width
屏幕可用工作区高度： window.screen.availHeight
屏幕可用工作区宽度： window.screen.availWidth 
*/
//test jquery
$(document).ready(function(){
function controlBar(ob){
	var itermId = $(ob).attr("tid");
	if($("#itermControlDiv").html()){
		$("#itermControlDiv").remove();
	}
	var itermName = $(ob).find("li:eq(1)").html();
	var tel = $(ob).find("li:eq(2)").html();
	var qq = $(ob).find("li:eq(3)").html();
	var msn = $(ob).find("li:eq(4)").html();
	var email = $(ob).find("li:eq(5)").html();
	var address = $(ob).find("li:eq(6)").html();
	var other = $(ob).find("li:eq(7)").html();
	var controlDiv= "<div id='itermControlDiv'><div id='closeDyWindow'><img src='style/window_close.png' onclick=\"closeDywindow('itermControlDiv')\" /></div><h4>"+itermName+"</h4><div>Tel:"+tel+"</div><div>QQ:"+qq+"</div><div>MSN:"+msn+"</div><div>email:"+email+"</div><div>地址:"+address+"</div><div>其他:"+other+"</div><input type='button' value='编辑' onclick=\"itermEd('"+itermId+"')\" /><input type='button' value='删除' onclick=\"itermDl('"+itermId+"')\" /></div>";
	$("body").append(controlDiv);
	var position = HHL.getPosition();
	var left = position.Tw/3;
	var windowHeight = $(window).height();
	var top = $(document).scrollTop()+windowHeight/3;
	$("#itermControlDiv").css({"left":left,"top":top})
}
window.controlBar=controlBar;

function closeDywindow(divId){
	$("#"+divId).remove();
}
window.closeDywindow=closeDywindow;

function itermDl(itermId){
	var conhandle = confirm("确认删除？");
	if(!conhandle){
		return false;
	}
	window.itermId = itermId;
	var url = "friendAjax.php";
	var data ={"itermId":itermId,"postType":1};
	$.ajax({
		type:"POST",
		data:data,
		url:url,
		success:function(data){
			if(data=="success"){
				HHL.newAlertMes("删除成功！","z-index:10","1500");
				$("#itermControlDiv").remove();
				$("#friendlist").find("div[tid='"+itermId+"']").remove();
			}
		}
	})
}
window.itermDl=itermDl;

function itermEd(id){
	window.location.href="friendmanage.php?id="+id;
}
window.itermEd=itermEd;
});

/**
date input configure
**/
$.extend(DateInput.DEFAULT_OPTS, {
  stringToDate: function(string) {
    var matches;
    if (matches = string.match(/^(d{4,4})-(d{2,2})-(d{2,2})$/)) {
      return new Date(matches[1], matches[2] - 1, matches[3]);
    } else {
      return null;
    };
  },
  dateToString: function(date) {
    var month = (date.getMonth() + 1).toString();
    var dom = date.getDate().toString();
    if (month.length == 1) month = "0" + month;
    if (dom.length == 1) dom = "0" + dom;
    return date.getFullYear() + "-" + month + "-" + dom;
  }
});

$("#my_specific_input").date_input();	
	//$("#my_specific_input").date_input();
$($.date_input.initialize);

$(document).ready(function(){
	var lis = $("#subnaviul li");
	for(var is=1;is<=lis.length;is++){
		//alert($("#subnaviul li:nth-child("+is+")").html());
		$("#subnaviul li:nth-child("+is+")").mouseover(function(){
			 
			changeNaviBg(this,0);
		});
		$("#subnaviul li:nth-child("+is+")").mouseout(function(){
			changeNaviBg(this,1);
		});	
	}
})
function changeNaviBg(needle,handle){
	if(handle==0){ 
		if($(needle).attr("class")=="subchangenvbg"){
			$(needle).css({"color":"red"});
			return true;
		}
		$(needle).css({"color":"red","background":"white"});
	}else{
		if($(needle).attr("class")=="subchangenvbg"){
			$(needle).css({"color":"black"});
			return true;
		}
		$(needle).css({"color":"black","background":"none"});;
	}
}





